package com.team.service.impl;

import com.team.dto.RegistrationDTO;
import com.team.entity.Registration;
import com.team.mapper.CompetitionMapper;
import com.team.mapper.RegistrationMapper;
import com.team.service.RegistrationService;
import com.team.vo.VolunteerInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class RegistrationServiceImpl implements RegistrationService {
    @Autowired
    private RegistrationMapper registrationMapper;
    @Autowired
    private CompetitionMapper competitionMapper;

    @Override
    public void addRegistrationInfo(RegistrationDTO registrationDTO) {
        int cid=registrationDTO.getCid();
        int studentId=registrationDTO.getStudentId();
        String note=registrationDTO.getNote();

        Registration registration = registrationMapper.getRegistration(studentId, cid);

        String stage=competitionMapper.getStage(cid);
        //不是报名阶段
        if(!"开始报名".equals(stage)){
            throw new IllegalStateException("该比赛不允许报名，当前阶段为：" + stage);
        }

        //已报名过该竞赛
        if(registration!=null){
            return;
        }

        Integer curPriority=registrationMapper.getCurPriority(studentId);
        Integer newPriority=(curPriority==null)?1:curPriority+1;

        Registration registration1 = Registration.builder()
                .studentId(studentId)
                .cid(cid)
                .priority(newPriority)
                .note(note)
                .build();
        registrationMapper.insert(registration1);

        // 更新竞赛的参与人数和一志愿人数
        competitionMapper.updateEntrantSum(cid);
        if(newPriority==1) competitionMapper.updateFirstSum(cid);
    }

    @Override
    public void deleteRegistrationInfo(int cid, int studentId) {
        // 查询学生报名信息
        Registration registration = registrationMapper.getRegistration(studentId, cid);
        if (registration == null) {
            throw new IllegalArgumentException("报名信息不存在");
        }

        // 获取删除前的报名优先级
        int deletedPriority = registration.getPriority();

        // 删除报名信息
        registrationMapper.deleteRegistration(studentId, cid);

        registrationMapper.updatePriority(registration.getPriority(),studentId);

        // 更新竞赛的报名人数
        competitionMapper.decrementEntrantsSum(cid);
        // 如果删除的是第一志愿报名，更新竞赛的第一志愿人数
        if (deletedPriority == 1) {
            competitionMapper.decrementFirstChoicesSum(cid);
            //获得当前学生的新的第一志愿报名内容
            Registration newFirstRegis=registrationMapper.getFirstRegis(studentId);
            //当前学生有一个新的第一志愿
            if(newFirstRegis!=null){
                int newCid=newFirstRegis.getCid();
                //该比赛的第一志愿人数+1
                competitionMapper.updateFirstSum(newCid);
            }
        }
    }

    @Override
    public List<VolunteerInfoVo> getVolunteer(int studentId) {
        return registrationMapper.getVolunteer(studentId);
    }

    @Override
    public void volunteerModify(int studentId, int status, int cid) {
        // 找当前学生的所有志愿内容
        List<Registration> registrations = registrationMapper.getRegistrations(studentId);

        // 找到当前竞赛志愿
        Registration curRegistration = null;
        for (Registration registration : registrations) {
            if (registration.getCid() == cid) {
                curRegistration = registration;
                break;
            }
        }

        if (curRegistration == null) {
            throw new IllegalArgumentException("该学生没有报名该竞赛");
        }

        // 当前志愿
        int curPriority = curRegistration.getPriority();  // 使用priority，而不是cid
        int total = registrations.size();
        //如果当前志愿为第一志愿，则该比赛的一志愿人数-1（不论status=-1/1）
        if(curPriority==1) competitionMapper.decrementFirstChoicesSum(cid);

        // 计算新的优先级
        int newPriority = (curPriority + status - 1 + total) % total + 1;

        // 修改当前志愿的优先级
        registrationMapper.updatePriority2(curRegistration.getRid(), newPriority);

        // 处理当前学生的其他志愿情况
        List<Registration> otherRegistrations = new ArrayList<>();
        for (Registration registration : registrations) {
            if (registration.getCid() != cid) {
                otherRegistrations.add(registration);
            }
        }

        // 根据新的优先级，调整其他志愿
        for (Registration registration : otherRegistrations) {
            // 计算并更新其他志愿的优先级
            int otherPriority = registration.getPriority();
            int newOtherPriority = (otherPriority + status - 1 + total) % total + 1;
            registrationMapper.updatePriority2(registration.getRid(), newOtherPriority);
            //如果其他信息的新的志愿为1，该比赛的第一志愿人数+1
            competitionMapper.updateFirstSum(registration.getCid());
        }
    }

}
